using System;
using System.Collections.Generic;

namespace Be.Timvw.Framework
{
    public static class TimeSpanHelper
    {
        private static readonly Dictionary<TimeUnit, TimeSpanCreatorDelegate> s_timeSpanCreatorDelegates;

        static TimeSpanHelper()
        {
            s_timeSpanCreatorDelegates = new Dictionary<TimeUnit, TimeSpanCreatorDelegate>();
            s_timeSpanCreatorDelegates.Add(TimeUnit.Day, TimeSpan.FromDays);
            s_timeSpanCreatorDelegates.Add(TimeUnit.Hour, TimeSpan.FromHours);
            s_timeSpanCreatorDelegates.Add(TimeUnit.Minute, TimeSpan.FromMinutes);
            s_timeSpanCreatorDelegates.Add(TimeUnit.Second, TimeSpan.FromSeconds);
        }

        public static TimeSpan Create(double value, TimeUnit timeUnit)
        {
            TimeSpanCreatorDelegate timeSpanCreatorDelegate;
            if(s_timeSpanCreatorDelegates.TryGetValue(timeUnit, out timeSpanCreatorDelegate))
            {
                return timeSpanCreatorDelegate(value);
            }

            throw new ArgumentException(string.Format("{0} is an unsupported TimeUnit", timeUnit));
        }

        #region Nested type: TimeSpanCreatorDelegate

        private delegate TimeSpan TimeSpanCreatorDelegate(double value);

        #endregion
    }
}